package com.browan.constellation.weibo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Login
{

	/**
	 * 验证用户是否合法
	 * 
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @return true 登录成功 其他false
	 * @throws IOException
	 */

	public static boolean doLogin(String username, String password) throws IOException
	{
		boolean ret = false;
		TockenManager manager = TockenManager.getInstence();
		HttpClient client = new HttpClient();
		Map<String, String> map = new HashMap<String, String>();
		map.put("action", "submit");
		map.put("oauth_token", manager.getTocken());
		map.put("oauth_callback", "oob");
		map.put("userId", username);
		map.put("passwd", password);
		String s = client.postMethod(UriManager.AUTHORIZE, map, "UTF-8");
		Pattern pattern = Pattern.compile("<b>(.*)?</b>");
		Matcher matcher = pattern.matcher(s);
		if (matcher.find())
		{
			s = matcher.group(1);
			manager.setAuth_number(s.split("：")[1]);
			ret = true;
		}
		return ret;
	}
}